package org.millioncall.yueyoga.admin.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.DateTimeUtils;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Group;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.Status;
import org.millioncall.yueyoga.admin.model.TestSection;
import org.millioncall.yueyoga.admin.model.User;
import org.millioncall.yueyoga.admin.service.MemberService;
import org.millioncall.yueyoga.admin.service.StatusService;
import org.millioncall.yueyoga.admin.service.TestPlaceMentService;
import org.millioncall.yueyoga.admin.service.TestService;
import org.millioncall.yueyoga.admin.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestAction extends BaseAction {
	private TestService testService;
	private MemberService memberService;
	private TestPlaceMentService testPlaceMentService;
	private UserService userService;
	private StatusService statusService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TestPlaceMentService getTestPlaceMentService() {
		return testPlaceMentService;
	}

	public void setTestPlaceMentService(
			TestPlaceMentService testPlaceMentService) {
		this.testPlaceMentService = testPlaceMentService;
	}

	public TestService getTestService() {
		return testService;
	}

	public void setTestService(TestService testService) {
		this.testService = testService;
	}

	// 根据会员Id，查询会员体能测试
	@RequestMapping(value = "/toMemberTest.jhtml")
	public String toMemberTest(ModelMap context, Integer id,
			HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, context);
		Map<String, Object> map1 = new HashMap<String, Object>();
		List<Integer> groupIds1 = new ArrayList<Integer>();
		for (Group group : this.getUser(request).getGroups()) {
			groupIds1.add(group.getId());
		}
		map1.put("groups", groupIds1);
		map1.put("id", id);
		map1.put("hallcode", this.getUser(request).getOrganizationId());
		Member member = this.memberService.findById(map1);
		context.put("member", member);
		List<Status> status = statusService.findByType(1);// 根据类型查询父类状态
		context.put("memberStatus", status);
		if (member.getSubstatus() != null && !member.getSubstatus().equals("")) {
			Status stat = statusService.findByCode(member.getSubstatus());
			context.put("statu", stat);
			status = statusService.findByParentCode(stat.getParentcode());
			context.put("subStatuses", status);
			stat = statusService.findByCode(stat.getParentcode());
			context.put("parentStatu", stat);
		}
		int start = 0;
		int limit = 10;
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		if (pageSize != null) {
			limit = Integer.parseInt(pageSize);
		}
		if (pageNo != null) {
			start = (Integer.parseInt(pageNo) - 1) * limit;
		}
		User user = this.getUser(request);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		context.put("id", id);
		String type = request.getParameter("inputType");
		if (type != null && !type.equals("") && !type.equals("0")) {
			map.put("type", Integer.parseInt(type));
		}
		String sortType = request.getParameter("sortType");
		if (sortType != null && !sortType.equals("")) {
			map.put("sortType", Integer.parseInt(sortType));
		} else {
			map.put("sortType", 1);
		}
		// 查询体能测试
		PaginatedList<TestSection> testSection = testService
				.findTestByMemberId(map, start, limit);
		context.put("testSection", testSection);
		// 查询负责人
		context.put("responsibles",
				userService.findUsersByGroupId("4", user.getOrganizationId()));
		context.put("sortType", sortType);

		context.put("resporsId", this.getUser(request).getId());
		
		return "test_add";
	}

	// 添加体能测试
	@RequestMapping(value = "/addTest.jhtml")
	public void addTest(Map<String, Object> context, Integer id,
			String resporId, Integer sortType, Integer inputType,
			String inputResult, String testPro, HttpServletRequest request,
			HttpServletResponse response) {
		String msg = null;
		TestSection testSection = new TestSection();
		testSection.setName(testPro);
		if (resporId != null) {
			testSection.setResporId(resporId);
		}
		if (id != null) {
			testSection.setMemberId(id);
		}
		if (StringUtils.isNotBlank(inputResult)) {
			testSection.setResult(inputResult);
		}
		String time = DateTimeUtils.getInstance().format(new Date(),
				"yyyy-MM-dd HH:mm:ss");
		if (StringUtils.isNotBlank(time)) {
			testSection.setCreateTime(time);
		}
		if (testService.insert(testSection)) {
			msg = "true";
		} else {
			msg = "false";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	// 添加体能测试
	@RequestMapping(value = "/addTaskTest.jhtml")
	public void addTaskTest(String memberId, String resporId, String content,
			String name, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String msg = null;
		TestSection testSection = new TestSection();
		if (StringUtils.isNotBlank(name)||"".equals(name)) {
			testSection.setName(name);
		}
		if (StringUtils.isNotBlank(resporId)) {
			testSection.setResporId(resporId);
		}
		if (StringUtils.isNotBlank(memberId)) {
			testSection.setMemberId(Integer.valueOf(memberId));
		}
		if (StringUtils.isNotBlank(content)) {
			testSection.setResult(content);
		}
		String time = DateTimeUtils.getInstance().format(new Date(),
				"yyyy-MM-dd HH:mm:ss");
		testSection.setCreateTime(time);
		
		if (testService.insert(testSection)) {
			msg = "{'success':true, 'msg':'创建体能记录成功!'}";
		} else {
			msg = "{'success':false,'msg':'创建体能记录失败!'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	public StatusService getStatusService() {
		return statusService;
	}

	public void setStatusService(StatusService statusService) {
		this.statusService = statusService;
	}
}
